<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-3-26
 * Time: 19:14
 */

namespace backend\controllers;


use common\models\Adminuser;
use common\models\AuthAssignment;
use common\models\AuthItem;
use common\models\AuthItemChild;
use common\models\Permissin;
use yii\caching\DbDependency;
use yii\data\Pagination;
use yii\web\Controller;
use Yii;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\ViewAction;

class PermissionController extends Controller
{
    public function actionIndex()
    {
        $per = new Permissin();
        $per->checkPermission();
        $permissionModel = new AuthItem();
        $dataProvider = $permissionModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            // 'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    public function actionView($id)
    {
        $per = new Permissin();
        $per->checkPermission();
        if (is_numeric($id)) {
            $model = AuthItem::find()->where(['id' => $id])->one();
        } else {
            $model = AuthItem::find()->where(['name' => $id])->one();
        }

        return $this->render('view', [
            'model' => $model,
        ]);
    }

    public function actionCreate()
    {
        $per = new Permissin();
        $per->checkPermission();
        $model = new AuthItem();
        $model->created_at = time();
        $model->updated_at = time();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    protected function findModel($id)
    {

        if (($model = AuthItem::find()->where(['name' => $id])->one()) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('请求页面不存在');
        }
    }

    public function actionUpdate($id)
    {
        $per = new Permissin();
        $per->checkPermission();
        $model = $this->findModel($id);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    public function actionDelete($id)
    {
        $per = new Permissin();
        $per->checkPermission();
        $model = AuthItem::find()->where(['name' => $id])->one()->delete();
        //$model->delete();
        //$this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    public function actionAllot()
    {
        $role = AuthItem::find()->where(['type' => 1])->all();
        $pemis = AuthItem::find()->where(['type' => 2])->all();
        $roleAry = [];
        $arr = [];
        $permisAry = [];
        foreach ($role as $k => $v) {
            $list['name'] = $v->name;
            $list['description'] = $v->description;
            $roleAry[] = $list;
        }
        foreach ($pemis as $kk => $vv) {
            $permisAry[$vv->name] = $vv->description;
        }
        $permissionModel = new AuthItem();
        $dataProvider = $permissionModel->search(Yii::$app->request->queryParams);
        if ($_POST && $_POST['username'] && $_POST['newPri']) {
            $user = AuthItemChild::deleteAll(['parent'=>$_POST['username']]);

            foreach ($_POST['newPri'] as $k => $v){
               $model = new AuthItemChild();
                $model->parent = $_POST['username'];
                $model->child = $v;
                $model->save();
                //$model->updateCounters(['child'=>$v],['parent' => $_POST['username']] );
            }
            return $this->render('index',['dataProvider' => $dataProvider]);
        }
        return $this->render('privilege', ['data' => $roleAry, 'per' => $permisAry, 'a' => $arr]);
    }



}